<?php

    function villeDejaValidee($userid, $city_postal_code, $city_name, $country_name) {
        require_once("Config/connexion.php");

        $city = R::findOne('tg_cities', ' postal_code = :city_postal_code and name = :city_name', array(':city_postal_code'=> $city_postal_code, ':city_name'=>$city_name ));
        
        if ($city != null) {
            $score = R::find('tg_score', ' users_id = :userid and cities_id = :cities_id', array(':userid'=> $userid, ':cities_id'=>$city->id ));
            
            if($score != null) {
                return true;
            }
        }
      
        return false;
    }
    
    function ajoutScore($userid, $city_postal_code, $city_name, $country_name) {
        require_once("Config/connexion.php");

        $idCountry = 1;
        $idCity = 0;
    
        $referentiel = R::findOne('tg_referentiel', ' id_insee like :departement and city_name = :city_name', array(':departement'=> substr($city_postal_code,0,2)."%", ':city_name'=>$city_name ));
        
        if ($referentiel == null) {
            throw new Exception("Ville introuvable dans le référentiel");
        }
        
        // Calcul points sur echelle
        $nombrePoints = nombrePointsVille($referentiel->population);
        
        $city = R::findOne('tg_cities', ' postal_code = :city_postal_code and name = :city_name', array(':city_postal_code'=> $city_postal_code, ':city_name'=>$city_name ));
        
        if ($city == null) {
            $newCity = R::dispense('tg_cities');
            $newCity->countries_id = $idCountry;
            $newCity->name = $city_name;
            $newCity->postal_code = $city_postal_code;
            $newCity->referentiel_id = $referentiel->id;
            
            $idCity = R::store($newCity);
        }
        else {
            $idCity = $city->id;
        }
        
        $newScore = R::dispense('tg_score');
        $newScore->users_id = $userid;
        $newScore->cities_id = $idCity;
        $newScore->date = date("Y-m-d");
        $newScore->points = $nombrePoints;
        
        R::store($newScore);
        
        return array('points' => $nombrePoints, 'population' => $referentiel->population);
    }
    
    function nombrePointsVille($habitants) {
        
        if ($habitants >= 0 && $habitants < 500) {
            return 1000;
        }
  
        if ($habitants >= 500 && $habitants < 1000) {
            return 500;
        }
        
        if ($habitants >= 1000 && $habitants < 4000) {
            return 250;
        }
        
        if ($habitants >= 4000 && $habitants < 8000) {
            return 100;
        }
        
        if ($habitants >= 8000 && $habitants < 15000) {
            return 75;
        }
        
        if ($habitants >= 15000 && $habitants < 30000) {
            return 50;
        }
        
        if ($habitants >= 30000) {
            return 25;
        }
    }
?>
